package com.example.demo.Student;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.time.LocalDate;
import java.time.Month;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

@Service
public class StudentService {

    private final StudentRepository studentRepository;

    @Autowired
    public StudentService(StudentRepository studentRepository) {

        this.studentRepository = studentRepository;
    }

    public List<Student> getStudent() {
        return studentRepository.findAll();
    }

    public void addNewStudent(Student student) {
        Optional<Student> studentByEmail = studentRepository.findStudentByEmail(student.getEmail());

        if(studentByEmail.isPresent()){
            throw new IllegalStateException("Email exists.");
        }
        System.out.println(student);
        studentRepository.save(student);
    }

    public void deleteStudent(Long studentID) {
        boolean exists = studentRepository.existsById(studentID);
        if(!exists){
            throw new IllegalStateException("student with id "+
                    studentID + "does not exists.");
        }
        studentRepository.deleteById(studentID);
    }

    @Transactional
    public void updateStudent(Long studentID,
                              String name,
                              String email) {
       Student student = studentRepository.findById(studentID)
               .orElseThrow( ()-> new IllegalStateException(
                       "student with "+ studentID + " doesnot exists"));
       if (name != null && name.length() > 0 && !Objects.equals( student.getName(), name)){
            student.setName(name);
       }
       if (email != null && email.length() > 0 && !Objects.equals( student.getEmail(), email)){
           Optional<Student> studentOptional = studentRepository.findStudentByEmail( email );
           if (studentOptional.isPresent()){
               throw new IllegalStateException("email taken");
           }
           student.setEmail(email);
       }

    }
}
